package com.zlf.service;

import com.zlf.mapper.PointMapper;
import com.zlf.mapper.PointTransactionMapper;
import com.zlf.po.Point;
import com.zlf.po.PointTransaction;
import com.zlf.util.PointUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PointTransactionService{

    @Autowired
    private PointMapper pointMapper;

    @Autowired
    private PointTransactionMapper pointTransactionMapper;

    private void checkUser(String userId) throws Exception {
        Point point = pointMapper.selectById(userId);
        if (point == null) {
            throw new Exception("用户不存在");
        }
    }

    public List<PointTransaction> getPointTrans(String userId, Integer index) throws Exception {
        checkUser(userId);
        if (index.equals(PointUtils.addIndex)) {
            return pointTransactionMapper.getIncomeTrans(userId);
        } else if (index.equals(PointUtils.consumeIndex)) {
            return pointTransactionMapper.getExchangeTrans(userId);
        } else {
            return pointTransactionMapper.getAllTrans(userId);
        }
    }
}
